home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_1 / simeq < prev    next >
Internet Message Format  |  1995-03-31  |  7KB

  1. From comp.sys.handhelds Tue Jun 18 12:48:15 1991
  2. Path: seq!ecsgate!mcnc!taco!lll-winken!elroy.jpl.nasa.gov!sdd.hp.com!caen!news.cs.indiana.edu!noose.ecn.purdue.edu!en.ecn.purdue.edu!blair
  3. From: blair@en.ecn.purdue.edu (Marc E Blair)
  4. Newsgroups: comp.sys.handhelds
  5. Subject:  Solve Sim Eq with symbolics
  6. Message-ID: <1991Jun12.224853.20572@en.ecn.purdue.edu>
  7. Date: 12 Jun 91 22:48:53 GMT
  8. References: <1991Jun11.203416.12491@watdragon.waterloo.edu>
  9. Organization: Purdue University Engineering Computer Network
  10. Lines: 348
  11.  
  12. This is a newer version which allows symbolics in solving simeq and also
  13. simplifies answers further than the old program.
  14.  
  15. ->Q   This program toggles fraction mode (a little block appears to indicate
  16.       fraction mode) otherwise straight decimals will be used. Fractions can
  17.       eat up memory on lengthy calculations.
  18.  
  19. det   returns a determinant of a matrix. It behaves much like the routine  
  20.       Eliel Louzouen wrote a while back, although I wrote this with speed
  21.       as first priority. The approach I used resulted in a twelvefold 
  22.       increase in speed with numeric evaluation and a threefold increase in
  23.       symbolic evaluation than the original routines.
  24.  
  25. simeq solves a system of equations in matrix format... i.e. 4x+5y=9 and
  26.       3x+2y=10 would be entered { { 4 5 9 } { 3 2 10 } } and evaluated
  27.       returning { '32/7' '-(13/7)' } meaning x=32/7 and y=-13/7.
  28.       The program uses matrices to solve if there are less than five
  29.       variables, >5 results in row reduction. (this way a ten variable
  30.       equation takes 5 minutes, matrix methods would take 25 days)
  31.  
  32. rr    row reduction program to reduce matrices into row-echelon form.
  33.       symbolic row reduction is supported.
  34.  
  35. inv   invert a matrix using row reduction techniques
  36.  
  37. mec   expand and collect and evaluate all items in a matrix to their most
  38.       simple form
  39. -----  All other variables are subprograms.
  40.  
  41. I am not repsonsible for memory loss, hardware trouble, loss of math abilities,
  42. sudden shifts in the space-time continuum, or other problems which  might occur
  43. due to the use of this program but in all likelihood will never happen .
  44.  
  45. enjoy!   
  46. ----------------------->8--------->8---
  47. %%HP: T(3)A(R)F(.);
  48. DIR
  49.   \->q
  50.     \<<
  51.       IF QR
  52.       THEN '\->q\[]'
  53. DUP RCL SWAP PURGE
  54. '\->q' STO
  55.       ELSE '\->q' DUP
  56. RCL SWAP PURGE
  57. '\->q\[]' STO
  58.       END QR NOT
  59. 'QR' STO
  60.     \>>
  61.   Rr
  62.     \<< Dec \-> L S
  63.       \<< S L \161RR 0 1
  64. S
  65.         FOR A A L *
  66. A - 2 + PICK +
  67.         NEXT
  68.         IF ZRO?
  69. SWAP DROP NOT
  70.         THEN S L
  71. \161RR
  72.         END 1 L
  73.         FOR A S
  74. \->LIST L A - S * A +
  75. ROLLD
  76.         NEXT L
  77. \->LIST
  78.       \>>
  79.     \>>
  80.   det
  81.     \<< Dec DROP MNN
  82.     \>>
  83.   EC
  84.     \<< EVAL
  85.       DO DUP EXPAN
  86. DUP ROT
  87.       UNTIL SIZE
  88. SWAP SIZE ==
  89.       END
  90.       DO DUP COLCT
  91. DUP ROT
  92.       UNTIL SIZE
  93. SWAP SIZE ==
  94.       END
  95.     \>>
  96.   SIMEQ
  97.     \<< DUP Dec DUP
  98.       IF 6 <
  99.       THEN DUP2 1 -
  100.         IF ==
  101.         THEN DROP \->
  102. Ss
  103.           \<< 0 Ss
  104.             FOR Aa
  105. Ss DUP * Ss
  106.               FOR
  107. Bb Bb Aa + PICK Ss
  108. NEG
  109.               STEP
  110. Ss \->LIST Ss Ss 1 +
  111. * 1 + ROLLD
  112.             NEXT Ss
  113. Ss 1 + * DROPN Ss 1
  114. + ROLL \-> Cc
  115.             \<< Ss
  116. DUPN Ss \->LIST det \->
  117. Dd
  118.               \<<
  119. IF Dd ZRO? SWAP
  120. DROP NOT
  121. THEN 1 Ss
  122.   FOR Aa Ss DUPN Aa
  123. ROLL DROP Cc Aa
  124. ROLLD Ss \->LIST det
  125. Dd /
  126.     IF QR
  127.     THEN \->Q
  128.     END Ss 1 +
  129. ROLLD
  130.   NEXT Ss DROPN Ss
  131. \->LIST
  132. ELSE Ss DROPN
  133. "No Solution"
  134. END
  135.               \>>
  136.             \>>
  137.           \>>
  138.         ELSE *
  139. DROPN
  140. "BAD # OF EQS"
  141.         END SWAP
  142. DROP
  143.       ELSE * DROPN
  144. SM2
  145.       END
  146.     \>>
  147.   inv
  148.     \<< Dec \-> S L
  149.       \<< 0 L 1 -
  150.         FOR A 0 S 1
  151. -
  152.           FOR B A B
  153. == L S * L - 1 + A
  154. L * - ROLLD
  155.           NEXT
  156.         NEXT L S
  157.       \>> DUP + \-> L S
  158.       \<< S L \161RR 1 L
  159.         FOR A S 2 /
  160. \->LIST L A - S * A +
  161. S 2 / + ROLLD S 2 /
  162. DROPN
  163.         NEXT L
  164. \->LIST
  165.       \>>
  166.     \>>
  167.   MEC
  168.     \<< OBJ\-> \-> A
  169.       \<< 1 A 1 -
  170.         FOR B +
  171.         NEXT OBJ\-> \->
  172. S
  173.         \<< 1 S
  174.           FOR C EC
  175. S ROLLD
  176.           NEXT 1 A
  177.           FOR D S A
  178. / \->LIST S S A / D *
  179. - D + ROLLD
  180.           NEXT A
  181. \->LIST
  182.         \>>
  183.       \>>
  184.     \>>
  185.   SM2
  186.     \<< Rr 0 'ER' STO
  187. { } SWAP OBJ\-> \-> S
  188.       \<< 1 S
  189.         FOR A OBJ\->
  190. \-> L
  191.           \<< L S A -
  192. - ROLL
  193.             IF 1 \=/
  194.             THEN 1
  195. 'ER' STO
  196.             END S A
  197. - L + ROLL + S A -
  198. L 1 - + ROLLD 0 1 L
  199. 2 -
  200.             FOR C +
  201.             NEXT
  202.             IF 0 \=/
  203.             THEN 1
  204. 'ER' STO
  205.             END
  206.           \>>
  207.         NEXT
  208.         IF ER 1 ==
  209.         THEN DROP
  210. "NO SOLUTION"
  211.         END 'ER'
  212. PURGE
  213.       \>>
  214.     \>>
  215.   Dec
  216.     \<< OBJ\-> DUP TYPE
  217.       IF 5 ==
  218.       THEN EVAL
  219.       ELSE \-> L
  220.         \<< 1 L 1 -
  221.           FOR A +
  222.           NEXT OBJ\->
  223. L / L SWAP
  224.         \>>
  225.       END
  226.     \>>
  227.   ZRO?
  228.     \<< DUP TYPE 0
  229.       IF \=/
  230.       THEN 0
  231.       ELSE DUP
  232.         IF 0 \=/
  233.         THEN 0
  234.         ELSE 1
  235.         END
  236.       END
  237.     \>>
  238.   \161RR
  239.     \<< \-> L S
  240.       \<< 0 S 1 -
  241.         FOR A S L *
  242. A - DUP 1 + PICK \->
  243. F M1
  244.           \<< 1 S 1 -
  245.             FOR B F
  246. B L * - DUP 1 +
  247. PICK \-> C M2
  248.               \<< M2
  249. ZRO?
  250. IF NOT
  251. THEN DROP 0 L 1 -
  252.   FOR D C A + D -
  253. ROLL M1 0 'DOIT'
  254. STO ZRO?
  255.     IF NOT
  256.     THEN *
  257.     ELSE DROP 1
  258. 'DOIT' STO
  259.     END F A + D -
  260. PICK M2 ZRO?
  261.     IF NOT
  262.     THEN *
  263.     ELSE DROP 1
  264. 'DOIT' STO
  265.     END - EXPAN
  266. COLCT C A + D -
  267. ROLLD
  268.   NEXT
  269. ELSE DROP
  270. END
  271.               \>>
  272.             NEXT 1
  273. L
  274.             FOR Q S
  275. L * ROLL
  276.             NEXT
  277.           \>> 'DOIT'
  278. PURGE
  279.         NEXT 0 S 1
  280. -
  281.         FOR B L S B
  282. - * B - PICK S B -
  283. L * \-> D F
  284.           \<< 0 L 1 -
  285.             FOR C F
  286. C - ROLL
  287.               IF D
  288. TYPE 0 ==
  289.               THEN
  290. IF D 0 ==
  291. THEN \oo *
  292. ELSE D / COLCT
  293.   IF QR
  294.   THEN \->Q
  295.   END DUP TYPE 9 ==
  296. OVER EVAL DUP IP ==
  297. AND
  298.   IF DUP TYPE 0 ==
  299.   THEN
  300.     IF
  301.     THEN EVAL
  302.     END
  303.   ELSE DROP
  304.   END
  305. END
  306.               ELSE
  307. D / COLCT
  308.               END F
  309. C - ROLLD
  310.             NEXT
  311.           \>>
  312.         NEXT
  313.       \>>
  314.     \>>
  315.   MNN
  316.     \<< \-> Ss
  317.       \<<
  318.         IF Ss 3 ==
  319.         THEN 6 DUPN
  320. 6 DUPN ROT DROP 4
  321. ROLL * 3 ROLLD * -
  322. SWAP DROP 16 PICK *
  323. 16 ROLLD SWAP DROP
  324. 4 ROLL DROP 4 ROLL
  325. * 3 ROLLD * - 9
  326. PICK * 10 ROLLD
  327. DROP ROT DROP 4
  328. ROLL * 3 ROLLD * -
  329. * ROT DROP SWAP
  330. DROP SWAP - +
  331.         ELSE
  332.           IF Ss 2
  333. ==
  334.           THEN 4
  335. ROLL * 3 ROLLD * -
  336.           ELSE 1 Ss
  337.             FOR Aa
  338. Ss DUP DUP * SWAP -
  339. DUPN Ss DUP * Ss 2
  340. * - 0
  341.               FOR
  342. Bb Bb Aa + ROLL
  343. DROP Ss NEG
  344.               STEP
  345. Ss 1 - MNN Ss DUP *
  346. Ss - Aa + 1 + PICK
  347. * -1 Aa Ss + ^ * Ss
  348. Ss * 1 + ROLLD
  349.             NEXT Ss
  350. Ss * DROPN 1 Ss 1 -
  351.             FOR Aa
  352. +
  353.             NEXT
  354.           END
  355.         END
  356.       \>>
  357.     \>>
  358.   QR 0
  359. END
  360.  
  361.